package com.heatup.apt.management.task;

import com.heatup.apt.common.constants.TokenConstants;
import com.heatup.apt.common.dao.TokenMapper;
import com.heatup.apt.common.dao.redis.TokenRedisDao;
import com.heatup.apt.common.util.TemplateMsgUtil;
import com.heatup.apt.management.util.ManageConfigPropertiesUtil;
import com.heatup.apt.model.weixin.Token;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@EnableScheduling
public class TokenServiceTask {
	private Logger logger = LoggerFactory.getLogger(TokenServiceTask.class);

	@Autowired
	private TokenMapper tokenMapper;
	@Autowired
	private TokenRedisDao tokenRedisDao;
	
	private final static String VIALD_TIME = "7200";

	@Scheduled(cron = "0 */5 * * * ?")
	public void getToken() {
		String TOKEN_UPDATE_FLAG = ManageConfigPropertiesUtil.getString("token_update_flag").trim();
		try {
			if ("1".equals(TOKEN_UPDATE_FLAG)) {
				logger.info("==>>定时任务更新token：TokenServiceImpl getToken start ==========");
				Token token = tokenMapper.selectTokenByKey(TokenConstants.ACCESS_TOKEN_KEY);
				if (token != null) {
					logger.info("get db token is : " + token.getToken() + "createTime  is : " + token.getCreateTime());
					Date createTime = token.getCreateTime();
					long createTimeMillis = createTime.getTime();
					long i = (System.currentTimeMillis() - createTimeMillis) / 1000;
					if (i > 5400) {
						String tokenStr = TemplateMsgUtil.getAccessToken();
						if (tokenStr != null) {
							token.setToken(tokenStr);
							token.setCreateTime(new Date());
							token.setVaildTime(VIALD_TIME);
							tokenMapper.updateByPrimaryKey(token);
							// 保存token到redis里面
							try {
								if (token != null) {
									tokenRedisDao.setToken_vaild_time(5400);
									tokenRedisDao.save(TokenConstants.ACCESS_TOKEN_KEY, token);
								}

							} catch (Exception e) {
								e.printStackTrace();
								logger.error("=================将数据更新进redis失败==================="+ e.getMessage().toString());
							}
						}
					}
				} else {
					token = new Token();
					String tokenStr = TemplateMsgUtil.getAccessToken();
					token.setKey(TokenConstants.ACCESS_TOKEN_KEY);
					token.setToken(tokenStr);
					token.setCreateTime(new Date());
					token.setVaildTime(VIALD_TIME);
					tokenMapper.insertToken(token);
					logger.info("get webchat token is : " + token.getToken() + "createTime  is : " + token.getCreateTime());
					// 保存token到redis里面
					try {
						if (token != null) {
							tokenRedisDao.setToken_vaild_time(5400);
							tokenRedisDao.save(TokenConstants.ACCESS_TOKEN_KEY, token);
						}
					} catch (Exception e) {
						e.printStackTrace();
						logger.error("=================将数据更新进redis失败==================="+e.getMessage());
					}
				}
				logger.info("==>> 定时任务更新token结束：TokenServiceImpl getToken end ==========");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error("==>> 定时任务更新token异常：TokenServiceTask getToken is error! " + ex.getMessage().toString());
		}
	}

}
